package com.closer.server.biz.organization.validator;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.closer.server.biz.organization.dto.*;
import com.closer.server.mapper.organization.OrganizationMapper;
import com.closer.server.mapper.organization.OrganizationMemberMapper;
import com.closer.server.mapper.user.UserMapper;
import com.closer.server.support.exception.ErrorCode;
import com.closer.server.support.exception.ThrowUtils;
import org.springframework.stereotype.Component;

@Component
public class OrganizationValidator {

    private final UserMapper userMapper;

    private final OrganizationMapper organizationMapper;

    private final OrganizationMemberMapper memberMapper;

    public OrganizationValidator(UserMapper userMapper,OrganizationMapper organizationMapper,OrganizationMemberMapper memberMapper) {
        this.userMapper = userMapper;
        this.organizationMapper = organizationMapper;
        this.memberMapper = memberMapper;
    }

    public void validate(CreateRequest params){
        ThrowUtils.throwIf(StrUtil.hasBlank(params.getName(),params.getDescription()), ErrorCode.PARAMS_ERROR,"参数为空");
        ThrowUtils.throwIf(params.getCreatorId() <= 0, ErrorCode.PARAMS_ERROR,"创建者Id错误");
        ThrowUtils.throwIf(params.getCreatorId() == null, ErrorCode.PARAMS_ERROR,"创建者Id错误");
        ThrowUtils.throwIf(!params.getName().matches("^[a-zA-Z\u4e00-\u9fa50-9_]+$"), ErrorCode.PARAMS_ERROR, "组织名称只能包含字母、中文、数字和下划线");
        ThrowUtils.throwIf(!params.getDescription().matches("^[a-zA-Z\u4e00-\u9fa50-9_]+$"), ErrorCode.PARAMS_ERROR, "组织描述只能包含字母、中文、数字和下划线");
        ThrowUtils.throwIf(params.getName().length() < 4 || params.getName().length() > 30, ErrorCode.PARAMS_ERROR,"组织名称长度4-30位");
        ThrowUtils.throwIf(params.getDescription().length() > 1024, ErrorCode.PARAMS_ERROR,"组织描述长度最多1024位");
        ThrowUtils.throwIf(userMapper.getUserInfoById(params.getCreatorId())== null, ErrorCode.PARAMS_ERROR,"创建者不存在");
    }

    public void validate(UpdateRequest params){
        ThrowUtils.throwIf(StrUtil.hasBlank(params.getName(),params.getDescription()), ErrorCode.PARAMS_ERROR,"参数为空");
        ThrowUtils.throwIf(!params.getName().matches("^[a-zA-Z\u4e00-\u9fa50-9_]+$"), ErrorCode.PARAMS_ERROR, "组织名称只能包含字母、中文、数字和下划线");
        ThrowUtils.throwIf(!params.getDescription().matches("^[a-zA-Z\u4e00-\u9fa50-9_]+$"), ErrorCode.PARAMS_ERROR, "组织描述只能包含字母、中文、数字和下划线");
        ThrowUtils.throwIf(params.getName().length() < 4 || params.getName().length() > 30, ErrorCode.PARAMS_ERROR,"组织名称长度4-30位");
        ThrowUtils.throwIf(params.getDescription().length() > 1024, ErrorCode.PARAMS_ERROR,"组织描述长度最多1024位");
        ThrowUtils.throwIf(organizationMapper.existsOrganization(params.getId())== null, ErrorCode.PARAMS_ERROR,"组织不存在");
    }

    public void validate(LeaveRequest params){
        ThrowUtils.throwIf(!StpUtil.isLogin(), ErrorCode.OPERATION_ERROR,"用户未登录");
        ThrowUtils.throwIf(organizationMapper.existsOrganization(params.getOrganizationId())== null, ErrorCode.PARAMS_ERROR,"组织不存在");
        ThrowUtils.throwIf(userMapper.getUserInfoById(params.getId())== null, ErrorCode.PARAMS_ERROR,"用户不存在");
        ThrowUtils.throwIf(memberMapper.existsMember(params.getId(),params.getOrganizationId())==null, ErrorCode.PARAMS_ERROR,"用户不在组织中");
    }

    public void validate(JoinRequest params){
        ThrowUtils.throwIf(organizationMapper.existsOrganization(params.getOrganizationId())== null, ErrorCode.PARAMS_ERROR,"组织不存在");
        ThrowUtils.throwIf(userMapper.getUserInfoById(params.getUserId())== null, ErrorCode.PARAMS_ERROR,"用户不存在");
    }

    public void validate(SearchRequest params){
        if(params.getName() != null){
            ThrowUtils.throwIf(!params.getName().matches("^[a-zA-Z\u4e00-\u9fa50-9_]+$"), ErrorCode.PARAMS_ERROR, "组织名称只能包含字母、中文、数字和下划线");
            ThrowUtils.throwIf(params.getName().length() < 4 || params.getName().length() > 30, ErrorCode.PARAMS_ERROR,"组织名称长度4-30位");
        }
        if(params.getStatus()!= null){
            ThrowUtils.throwIf(params.getStatus() < 0 || params.getStatus() > 1, ErrorCode.PARAMS_ERROR,"组织状态错误");
        }

    }

}
